package com.lovo.frame;

import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegisterPanel extends JPanel implements ActionListener {
    private ATM frame;
    private JLabel welcome = new JLabel("欢迎注册中国银行账户");
    private JLabel accountLabel = new JLabel("账号：");
    private JLabel pwdLabel = new JLabel("密码：");
    private JLabel pwdConfirmLabel = new JLabel("确认密码：");
    private JTextField accountTextField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private JPasswordField pwdConfirmTextField = new JPasswordField();
    private JButton backBtn = new JButton(Util.getIcon("/images/buttonOff.jpg"));
    private JButton registerBtn = new JButton(Util.getIcon("/images/buttonLogin.jpg"));
    private Font font = new Font("宋体", Font.BOLD, 25);

    private UserService userService = new UserServiceImpl();


    public RegisterPanel(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        //欢迎设置
        welcome.setBounds(350, 10, 400, 50);
        welcome.setFont(font);
        this.add(welcome);
        //账户提示设置
        accountLabel.setBounds(40, 400, 100, 50);
        accountLabel.setFont(font);
        this.add(accountLabel);
        //账户输入框设置
        accountTextField.setBounds(200, 400, 200, 50);
        accountTextField.setFont(font);
        this.add(accountTextField);

        pwdLabel.setBounds(40, 450, 100, 50);
        pwdLabel.setFont(font);
        this.add(pwdLabel);

        pwdField.setBounds(200, 450, 200, 50);
        pwdField.setFont(font);
        this.add(pwdField);

        pwdConfirmLabel.setBounds(40, 500, 200, 50);
        pwdConfirmLabel.setFont(font);
        this.add(pwdConfirmLabel);

        pwdConfirmTextField.setBounds(200, 500, 200, 50);
        pwdConfirmTextField.setFont(font);
        this.add(pwdConfirmTextField);

        registerBtn.setBounds(40, 550, 140, 50);
        registerBtn.addActionListener(this);
        this.add(registerBtn);

        backBtn.setBounds(240, 550, 140, 50);
        backBtn.addActionListener(this);
        this.add(backBtn);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == registerBtn) {
            // 获取输入的账号
            String account = accountTextField.getText();
            // 获取输入的密码，与确认密码
            String pwd = String.valueOf(pwdField.getPassword());
            String confirmPwd = String.valueOf(pwdConfirmTextField.getPassword());

            try {
                userService.register(account,pwd,confirmPwd);
                JOptionPane.showMessageDialog(this, "注册成功");
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this,exception.getMessage());
            }
            //返回登录界面
            frame.switchPanel("login");

        } else if (e.getSource() == backBtn) {//返回登录界面
            frame.switchPanel("login");
        }
    }
}
